//
//  RegisteredViewController.m
//  Sunexpress
//
//  Created by 筱亮 on 2017/5/10.
//  Copyright © 2017年 曹亮. All rights reserved.
//

#import "RegisteredViewController.h"
#import "RegisteredView.h"
#import "TheMacro.h"
#import  <SVProgressHUD.h>
#import <SDAutoLayout.h>
#import "RegistereModel.h"
@interface RegisteredViewController ()
{
    int timeTick;
    NSTimer* timer;
    BOOL isBtn;
    RegisteredView * view;
}
@end

@implementation RegisteredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isBtn =YES;
    self.title = @"注册";
    view = [[RegisteredView alloc]initWithFrame:self.view.frame];
    view.backgroundColor = [UIColor colorWithWhite:0.951 alpha:1.000];
    self.view = view;
    //获取验证码：
    
    [view.confirmBtn addTarget:self action:@selector(confir) forControlEvents:UIControlEventTouchUpInside];
    
//注册
    
    [view.registeredBtn addTarget:self action:@selector(zhuce) forControlEvents:UIControlEventTouchUpInside];
    
    
}

//注册
-(void)zhuce
{

    
      [NetWorkingRequest postWithURLString:[NSString stringWithFormat:@"http://daohe.apic.1by1.cc/passport/register?mobile=%@&password=%@&sms_code=%@",view.pohoneNumber.text,view.confirmPassword.text,view.verificationText.text] parameters:nil success:^(NSDictionary *data) {
          NSLog(@"+++%@",data);
          
          RegistereModel * mo = [[RegistereModel alloc]init];
          
          [mo setValuesForKeysWithDictionary:data];
          
          RegistereModel_mo * moo = [[RegistereModel_mo alloc]init];
          
          [moo setValuesForKeysWithDictionary:mo.data];
          
          NSLog(@"=-=-=-=-=-=%@",moo.is_locked);
          
          if ([view.password.text isEqualToString:view.confirmPassword.text]) {
              if (moo.is_locked == nil) {
                  [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",mo.errmsg]];
                  
              }
              else if ([moo.is_locked isEqualToString:@"0"]){
                  [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                  [self.navigationController popViewControllerAnimated:YES];
                  
              }

          }
          else{
          
              [SVProgressHUD showErrorWithStatus:@"密码不符请重试"];
          }
          
          
          
        
    } failure:^(NSError *error) {
        
        NSLog(@"---");
    }];
    
}


//获取验证码
-(void)confir
{
  
        if (view.pohoneNumber.text.length==0 ) {
            [SVProgressHUD showErrorWithStatus:@"请填写手机号"];
            isBtn=YES;
        }
        
        else if(view.pohoneNumber.text.length!=0 ){
             view.confirmBtn.enabled=NO;
            NSString*strurl = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/passport/send_sms_code?mobile=%@&type_id=1",view.pohoneNumber.text];
            
            
            
            timeTick =  3;
            timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            [NetWorkingRequest getWithURLString:strurl parameters:nil success:^(NSDictionary *data) {
                
                
            } failure:^(NSError *error) {
                
            }];
            
        }
       
   
}
-(void)timeFireMethod
{
    timeTick--;
    if(timeTick==0){
        [self BtnYanzhengma];
        view.confirmBtn.enabled=YES;
        [timer invalidate];
    }else
    {
        view.confirmBtn.backgroundColor = [UIColor grayColor];
        [view.confirmBtn setTitle:[NSString stringWithFormat:@"%d秒后再试",timeTick] forState:0];
        view.confirmBtn.enabled=NO;
        
    }


    }
//验证码
-(void)BtnYanzhengma
{
    
    
    
     view.confirmBtn.backgroundColor = [UIColor orangeColor];
    
    [view.confirmBtn setTitle:@"获取验证码" forState:0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
